/**
 * Created with IntelliJ IDEA.
 * User: liulinhui
 * Date: 18-10-8
 * Time: 下午7:16
 * Description: commonUtils.js
 */
import {getStorage} from '../utils/storage'
import crypto from '../utils/crypto.js'
import axios from 'axios'
const constants = require('../utils/constants.js').default;

/**
 * 校验密码的合法性
 * @param secret 密码
 * @return {boolean}
 */
export function VerifyPassword(secret) {
  let str = secret;
  if (str.length > 7) {
    let reg = /^[a-zA-Z0-9]+$/;
    return reg.test(str);
  } else {
    return false
  }
}

/**
 * 校验账户名的合法性
 * @param username 账户名
 * @return {boolean}
 */
export function AccountUser(username) {
  let str = username;
  if (str.length ==12) {
    let reg = /^[a-zA-Z1-5]+$/;
    return reg.test(str);
  } else {
    return false
  }
}

/**
 * 获取货币实时价格
 * @param cb
 */
export function getPrice(cb) {
  let api = 'https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD,CNY';
  axios.get(api)
    .then(function (response) {
      cb(null, response.data)
    })
}

// 保留四位小数字符串
export function getforNumber(num){
          return Number(num).toFixed(4).toString();
      };
/**
 * 校验密码
 * @param secret 密码
 * @returns {boolean} true:校验成功
 */
export function validateSecret(secret) {
  let oldSecretPub = getStorage(constants.storageKeys.LOGIN_PUB);
  let pubKey = crypto.getPrivateAndPublicKeyFromSecret(secret).publicKey;
  return !!(oldSecretPub && pubKey && oldSecretPub === pubKey);
}

export function SecondToDate(msd) {
  let time =msd
  if (null != time && "" != time) {
    if (time > 60 && time < 60 * 60) {
      time = parseInt(time / 60.0) + "分钟" + parseInt((parseFloat(time / 60.0) -
        parseInt(time / 60.0)) * 60) + "秒";
    }
    else if (time >= 60 * 60 && time < 60 * 60 * 24) {
      time = parseInt(time / 3600.0) + "小时" + parseInt((parseFloat(time / 3600.0) -
        parseInt(time / 3600.0)) * 60) + "分钟" +
        parseInt((parseFloat((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60) -
          parseInt((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60)) * 60) + "秒";
    } else if (time >= 60 * 60 * 24) {
      time = parseInt(time / 3600.0/24) + "天" +parseInt((parseFloat(time / 3600.0/24)-
        parseInt(time / 3600.0/24))*24) + "小时" + parseInt((parseFloat(time / 3600.0) -
        parseInt(time / 3600.0)) * 60) + "分钟" +
        parseInt((parseFloat((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60) -
          parseInt((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60)) * 60) + "秒";
    }
    else {
      time = parseInt(time) + "秒";
    }
  }
  return time;
}
